﻿using System.Services;
using System.Threading.Tasks;
using Windows.Devices.Sensors;

namespace System.Sensors
{
    public interface ISimpleOrientationSensorService : ISensorService<SimpleOrientationSensor, SimpleOrientationSensorReading>
    {
    }

    [AutoMessagePublishing]
    [ExportService("SimpleOrientationSensorService", "", typeof(ISimpleOrientationSensorService), int.MaxValue)]
    public class SimpleOrientationSensorService : SensorServiceBase<SimpleOrientationSensor, SimpleOrientationSensorReading>, ISimpleOrientationSensorService
    {
        public SimpleOrientationSensorService()
        {
        }

        public override void StartService()
        {
            Sensor = SimpleOrientationSensor.GetDefault();
            base.StartService();
        }

        public override void Subscribe()
        {
            if (IsSensorAvailable)
                Sensor.OrientationChanged += Sensor_OrientationChanged;

            base.Subscribe();
        }

        public override void Unsubscribe()
        {
            if (IsSensorAvailable)
                Sensor.OrientationChanged -= Sensor_OrientationChanged;
            base.Unsubscribe();
        }

        void Sensor_OrientationChanged(SimpleOrientationSensor sender, SimpleOrientationSensorOrientationChangedEventArgs args)
        {
            if (EventAggregator != null)
                EventAggregator.Publish<SimpleOrientationServiceReadingMessage>(
                    new SimpleOrientationServiceReadingMessage(
                        this,
                        new SimpleOrientationSensorReading()
                        {
                            Orientation = args.Orientation,
                            Timestamp = args.Timestamp
                        }));
        }

        protected override async Task<SimpleOrientationSensorReading> GetSensorData()
        {
            var orientation = Sensor.GetCurrentOrientation();
            return new SimpleOrientationSensorReading()
            {
                Orientation = orientation,
                Timestamp = DateTimeOffset.Now
            };
        }

        public override uint GetMinimumReportInterval()
        {
            return 0;
        }

        public override uint GetReportInterval()
        {
            return 0;
        }

        public override void SetReportInterval(uint value)
        {
        }
    }

    public class SimpleOrientationServiceReadingMessage : OrientationSensorReadingMessageBase<SimpleOrientationSensorReading>
    {
        public SimpleOrientationServiceReadingMessage(object sender, SimpleOrientationSensorReading value)
            : base(sender, value)
        {
        }
    }


    // Summary:
    //     Provides data for the sensor reading–changed event.
    public sealed class SimpleOrientationSensorReading
    {
        // Summary:
        //     Gets the current sensor orientation.
        //
        // Returns:
        //     The current sensor orientation.
        public SimpleOrientation Orientation { get; internal set; }
        //
        // Summary:
        //     Gets the time of the current sensor reading.
        //
        // Returns:
        //     The time of the current sensor reading.
        public DateTimeOffset Timestamp { get; internal set; }
    }
}
